package com.changan.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changan.entity.User;
import com.changan.exception.BusinessException;
import com.changan.mapper.UserMapper;
import com.changan.response.ResultCode;
import com.changan.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changan.utils.JWTUtils;
import com.changan.utils.MD5Utils;
import com.changan.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author NieChangan
 * @since 2022-03-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> findUserList(Page<User> page, UserVO userVO) {
        List<User> userList = this.baseMapper.findUserList(page, userVO);
        page.setRecords(userList);
        return page;
    }

    @Override
    public void exportUserExcel(HttpServletResponse response, List<User> users) throws IOException {
        //----- 写入excel文件
        //1.定义文件的名称
        String exportFileName = "TbUserInfo.xlsx";
        //2.设置响应头的字符集(如果没有设置,导出来可能就是乱码)
        response.setCharacterEncoding("UTF-8");
        //3.设置导出的格式
        response.setContentType("application/vnd.ms-excel");
        //4.设置导出文件的名称
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        //5.执行导出的操作
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("用户信息")
                //自动列宽(都是参数 自动列宽 合并单元格... 参数的配置根据官方文档来)
                //https://www.yuque.com/easyexcel/doc/write#orUi8 右边的导航栏可以看到
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                //6.需要导出的数据内容
                .doWrite(users);
    }

    @Override
    public List<User> findUsers(UserVO userVO) {
        return this.baseMapper.findUsers(userVO);
    }

    /**
     * 功能描述: <br>
     * 〈在这里需要生成token并且进行返回〉
     * @Param: [username, password]
     * @Return: java.lang.String
     * @Author: Mr.Nie
     * @Date: 2022/3/14 14:12
     */
    @Override
    public String login(String username, String password) {
        //定义一个token进行返回
        String token = null;
        //1.根据用户名查询用户信息
        User user = this.baseMapper.loadUserByUsername(username);
        if(user!=null){
//2.需要获取用户的密码跟盐
            String salt = user.getSalt();
            String userPassword = user.getPassword();
            //3.对原始密码进行加密加盐的操作
            String encryptionPassword = MD5Utils.md5Encryption(password, salt);
            //4.对比数据库的密码和加密之后的密码,判断是否相等
            if(userPassword.equals(encryptionPassword)){
                //5.相等则生成token
                token = JWTUtils.sign(username, password);
                //6.还需要将token存入到shiro的登录管理器中
            }else{
                throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                        ResultCode.USER_CREDENTIALS_ERROR.getMessage());
            }
        }else{
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return token;
    }
}
